package scala.test

import scala.collection.immutable
import scala.io.Source

object Test04 {
  def main(args: Array[String]): Unit = {
    /**
      * 2、统计总分大于年级平均分的学生
     */
    //读取数据
    val lists: List[String] = Source.fromFile("data/score.txt").getLines().toList
    //取数据
    val datas: List[(String, String, Int)] = lists.map(i => {
      val split: Array[String] = i.split(",")
      val id: String = split(0)//id
      val subject: String = split(1)//科目
      val score: Int = split(2).toInt//成绩
      (id,subject,score)
    })
    //根据id分组
    val stringToTuples: Map[String, List[(String, String, Int)]] = datas.groupBy(_._1)
    //计算总分
    val stringToInt: Map[String, Int] = stringToTuples.mapValues(f =>f.map(i =>i._3).sum)
    //求年级平均分
    val ints: immutable.Iterable[Int] = stringToInt.map(f => f._2)
    val scoreSum: Int = ints.sum
    val length: Int = stringToTuples.toList.length
    val avgScore: Int = scoreSum/length
    //过滤成绩，保留总分大于年级平均分
    stringToInt.toList.filter(i =>i._2>avgScore).toList.sortBy(f=>f._1).foreach(println)
  }
}
